/**
 * Traffic Controller
 */

angular.module('NxtMap')
  .controller('TrafficCtrl', ['$rootScope', '$scope', '$cookieStore', '$timeout', '$http', 'MapDiagram', 'MapAnalysis', 'MapService', 'ConstService', TrafficCtrl]);

function TrafficCtrl($rootScope, $scope, $cookieStore, $timeout, $http, MapDiagram, MapAnalysis, MapService, ConstService) {
  $rootScope.loop = 0;
  $rootScope.setShowMap(true);
  MapService.clearRfidPonit();
  $scope.isFold = false;
  $scope.toggle = function (){
    $scope.isFold = !$scope.isFold;
  }

  //查询案件类型选项
  $scope.types = [ConstService.types[2]];  
  $scope.selectedTypes = [];
  $scope.selectedSubTypes = [];
  $scope.showType = 0;
  //类别多选框操作
  $scope.checkSubType = function (name){
    var index = $scope.selectedSubTypes.indexOf(name);
    if(index < 0){
      $scope.selectedSubTypes.push(name);
    }
    else{
      $scope.selectedSubTypes.splice(index, 1);
    }
  }
  $scope.haveSubType = function (name){
    return $scope.selectedSubTypes.indexOf(name) > -1;
  }
  $scope.checkShowType = function (typeIndex) {
    $scope.showType = $scope.showType == typeIndex ? -1 : typeIndex;
  }
  $scope.checkType = function (typeIndex, name) {
    var index = $scope.selectedTypes.indexOf(name);
    if(index < 0){
      $scope.selectedTypes.push(name);
      for(var i = 0; i < $scope.types[typeIndex].children.length; i++){
        var subName = $scope.types[typeIndex].children[i];
        var subIndex = $scope.selectedSubTypes.indexOf(subName);
        if(subIndex < 0){
          $scope.selectedSubTypes.push(subName);
        }
      }
    }
    else{
      $scope.selectedTypes.splice(index, 1);
      for(var i = 0; i < $scope.types[typeIndex].children.length; i++){
        var subName = $scope.types[typeIndex].children[i];
        var subIndex = $scope.selectedSubTypes.indexOf(subName);
        if(subIndex > -1){
          $scope.selectedSubTypes.splice(subIndex, 1);
        }
      }
    }
    
  }
  $scope.haveType = function (name){
    return $scope.selectedTypes.indexOf(name) > -1;
  }

  //选项列表数据
  $scope.anaTypes = ConstService.anaTypes;
  $scope.hourList = ConstService.hourList;
  $scope.dateList = ConstService.dateList;
  $scope.timeList = ConstService.timeList;
  $scope.weekList = ConstService.weekList;
  $scope.stations = ['海宁公安局'].concat(ConstService.stations);

  //星期复选
  $scope.selectedWeek = ['1', '2', '3', '4', '5', '6', '7'];
  $scope.checkWeek = function (value) {
    var index = $scope.selectedWeek.indexOf(value);
    if(index < 0){
      $scope.selectedWeek.push(value);
    }
    else{
      $scope.selectedWeek.splice(index, 1);
    }
  }
  $scope.haveWeek = function (value) {
    return $scope.selectedWeek.indexOf(value) > -1;
  }

  //查询条件参数
  $scope.option = {
    number: 10,
    opacity: 5,
    size: 5,
    heightBase: 0,
    anaType: 'space',
    dateType: 'custom',
    startDate: new Date('2016/03/01'),
    endDate: new Date('2016/12/31'),
    hourType: '全天',
    startHour: '00',
    endHour: '23',
    station: '海宁公安局',
    spaceCell: 'station',
    timeCell: 'hour',
    showPoint: false
  };
  //计算后的地图数据
  $scope.computeData = {}; 
  //地图几何体数据 
  $scope.infoData = [];
  //原始数据
  $scope.originData = [];
  //图表数据
  $scope.dataMap = {};
  $scope.promiseList = null;
  $scope.showInfo = -1;
  $scope.showType = 0;

  MapService.clearMap();
  $scope.submit = function () {
    if($scope.selectedSubTypes.length > 0) {
      var qdate = getDate($scope.option.dateType);
      $scope.qhour = getHour($scope.option.hourType);

      var type = '';
      for(var i = 0; i < $scope.selectedSubTypes.length; i++){
        type = type + '"' + $scope.selectedSubTypes[i] + '",';
      }

      var station_str = '';
      if($scope.option.station != '海宁公安局') {
        station_str = ' , "bumen": "' + $scope.option.station + '"';
      }


      var url = $rootScope.url_origin + '/jingqings/crossCombine';
      $http({
        method: 'POST',
        url: url,
        data: {
          option: {
            where: {
              and: [
                {BJSJ: {between: [qdate[0], qdate[1]]}},
                {BJXLMC: {inq: $scope.selectedSubTypes}},
                {XQ: {inq: $scope.selectedWeek}},
                {XS: {between: [$scope.qhour[0], $scope.qhour[1]]}}
              ]
            }
          }
        }
      }).success(function (data){
        
        $scope.originData = data;
        $scope.computeData = MapAnalysis.countCross(data);
        MapService.clearMap();
        $scope.infoData1 = MapService.drawCross($scope.computeData);
        $scope.infoData = MapService.drawRoad($scope.computeData);
        $scope.dataMap = MapAnalysis.countType(data, $scope.qhour);
        // MapDiagram.drawBar('chart1', dataMap);
        MapDiagram.drawLine('chart2', $scope.dataMap);
        // MapService.drawPointGeometry($scope.originData);
        $scope.isSummaryFold = false;
      });
      $scope.isSelectorFold = true;
    }
  }

  $scope.checkType(0, '交通事件');
  $scope.submit();

  //切换地图
  $scope.mapType = 0;
  $scope.changeMap = function (value) {
    $scope.mapType = MapService.changeMap($scope.mapType, value);
  } 
  // $scope.changeMap(1);

  //刷新位置
  $scope.flyHome = function () {
    MapService.flyHome();
    // MapService.setPosition(120.55001, 30.43014, 0.15, - Math.PI / 2);
  }
  $scope.flyHome();

  // 添加实体的点击操作
  var scene = MapService.viewer.scene;
  // handler = new Cesium.ScreenSpaceEventHandler(scene.canvas);
  MapService.handler.removeInputAction(Cesium.ScreenSpaceEventType.LEFT_CLICK);
  MapService.handler.setInputAction(function(movement) {
    var pickedObject = scene.drillPick(movement.position);
    var checked = -1;
    if (Cesium.defined(pickedObject)) {
      for(var i = 0; i < pickedObject.length; i++){
        checked = pickedObject[i].id.name;
      }
    }
    if($scope.showInfo != checked) {
      $scope.showType = 0;
      if(checked < 0 || checked == undefined){
        $scope.showInfo = -1;
        $scope.dataMap = MapAnalysis.countType($scope.originData, $scope.qhour);
      }
      else if(typeof(checked) == 'string') {
        $scope.showType = 1;
        $scope.showInfo = checked.substring(2);
        $scope.dataMap = MapAnalysis.countType($scope.infoData1[checked.substring(2)].objects, $scope.qhour);
      }
      else {
        $scope.showInfo = checked;
        $scope.dataMap = MapAnalysis.countType($scope.infoData[checked].objects, $scope.qhour);
        // MapService.drawPointGeometry($scope.infoData[checked].objects);
      }
      // MapDiagram.drawBar('chart1', dataMap);
      MapDiagram.drawLine('chart2', $scope.dataMap);
    }
    $scope.$apply();
  }, Cesium.ScreenSpaceEventType.LEFT_CLICK);


  function getDate(dateType) {
    var start = new Date();
    var end = new Date();
    switch (dateType){
      case 'yesterday':
        start.setDate(start.getDate() - 1);
        end.setDate(end.getDate() + 1);
        break;
      case 'lastweek':
        start.setDate(start.getDate() - start.getDay() - 7);
        end.setDate(end.getDate() - end.getDay());
        break;
      case 'lastmonth':
        start.setDate(1);
        start.setMonth(start.getMonth() - 1);
        end.setDate(1);
        break;
      case 'thisweek':
        start.setDate(start.getDate() - start.getDay());
        end.setDate(end.getDate() + 1);
        break;
      case 'thismonth':
        start.setDate(1);
        end.setDate(end.getDate() + 1);
        break;
      case 'custom':
        start = $scope.option.startDate;
        end = $scope.option.endDate;
        // end.setDate(end.getDate() + 1);
        break;
    }
    return [start.format('yyyy/MM/dd'), end.format('yyyy/MM/dd')];
  }

  function getHour(hourType) {
    if(hourType == '自选时间') {
      return [$scope.option.startHour, $scope.option.endHour]; 
    }
    for(var i = 0; i < $scope.timeList.length; i++){
      if(hourType == $scope.timeList[i].name) {
        return [$scope.timeList[i].start, $scope.timeList[i].end];
      }
    }
  }
}